package com.lin.views;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.DistanceUtil;
import com.lin.utils.Parameters;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.app.AppContext;
import com.yunfeng.supercall.bean.RecentCallsBean;
import com.yunfeng.supercall.bean.NewSuperFriendsBean;
import com.yunfeng.supercall.bean.NewSuperFriendsBean;
import com.yunfeng.supercall.bean.UserBean;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewForApplyFriends extends ListView{
	MyAdapter mFocusGameAdapter;
	Context mContext;
	DisplayImageOptions options;

	OnItemListViewEventListener mOnItemListViewEventListener;
	UserBean userBean;
	public ListViewForApplyFriends(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		options = new DisplayImageOptions.Builder()
		.displayer(new RoundedBitmapDisplayer(context.getResources().getDimensionPixelSize(R.dimen.corners_avatar)))
		.showImageOnFail(R.drawable.default_avatar)
		.showImageOnLoading(R.drawable.default_avatar)
		.cacheInMemory(true)
		.cacheOnDisk(true)
		.considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565)
		.build();
		
		userBean = BaseActivity.getUser(mContext);

	}

	public void setOnItemListViewEventListener(OnItemListViewEventListener mOnItemListViewEventListener) {
		this.mOnItemListViewEventListener = mOnItemListViewEventListener;
	}

	public interface OnItemListViewEventListener {
		public void onItemAvatarLisener(NewSuperFriendsBean maListInfo, int x, int y);
		public void onItemClickListener(NewSuperFriendsBean maListInfo, int x, int y);
		public void onItemStateListener(NewSuperFriendsBean maListInfo, int x, int y);
	}


	public void setDataList(ArrayList<NewSuperFriendsBean> mFocusGameList) {
		if (mFocusGameAdapter == null) {
			mFocusGameAdapter = new MyAdapter(getContext(), mFocusGameList);
			setAdapter(mFocusGameAdapter);
		} else {
			mFocusGameAdapter.setmFocusGameList(mFocusGameList);
			mFocusGameAdapter.notifyDataSetChanged();
		}
	}

	private class MyAdapter extends BaseAdapter implements OnClickListener{

		LayoutInflater mLayoutInflater;
		ArrayList<NewSuperFriendsBean> mDataList;
		int mSize;




		public MyAdapter(Context context,
				ArrayList<NewSuperFriendsBean> mDataList) {
			// TODO Auto-generated constructor stub
			this.mDataList = mDataList;
			mLayoutInflater =  LayoutInflater.from(mContext);
		}

		public ArrayList<NewSuperFriendsBean> getDataList() {
			return mDataList;
		}

		public void setmFocusGameList(ArrayList<NewSuperFriendsBean> mFocusGameList) {
			this.mDataList = mFocusGameList;
		}


		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			mSize = mDataList.size();
			return mSize;
		}


		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}






		@Override
		public NewSuperFriendsBean getItem(int position) {
			// TODO Auto-generated method stub
			return mDataList.get(position);
		}

		@SuppressLint("NewApi")
		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder viewholder = null;

			if (convertView == null) {
				viewholder = new ViewHolder();
				convertView = mLayoutInflater.inflate(
						R.layout.list_item_focus, null);
				viewholder.ivAvatar = (ImageView) convertView.findViewById(R.id.avatar);
				viewholder.tvUserName = (TextView) convertView.findViewById(R.id.username);
				viewholder.tvSubTitle = (TextView) convertView.findViewById(R.id.subtitle);
				viewholder.tvState = (TextView) convertView.findViewById(R.id.textview_state);
				viewholder.ivSex = (ImageView) convertView.findViewById(R.id.sex);
				convertView.setTag(viewholder);

			} else {
				viewholder = (ViewHolder) convertView.getTag();
			}

			NewSuperFriendsBean base = getItem(position);
			
			if(base != null && base.getType() == 2) {
				viewholder.ivSex.setVisibility(View.GONE);
			} else {
				viewholder.ivSex.setVisibility(View.VISIBLE);
			}
			viewholder.tvUserName.setText(base.getUsername());
			ImageLoader.getInstance().displayImage(Parameters.SERVER_PIC_URL + base.getUserphoto(), viewholder.ivAvatar, options);
			viewholder.ivAvatar.setOnClickListener(this);

			viewholder.tvSubTitle.setText(base.getNote());
			viewholder.tvState.setOnClickListener(this);
			
			viewholder.tvState.setTag(base);
			viewholder.ivAvatar.setTag(base);
			
			switch (base.getState()) {
			case 0:
				if(userBean.getUserid() == base.getOtherid()) {
					viewholder.tvState.setText("接受请求");
					viewholder.tvState.setTextColor(Color.GRAY);
					viewholder.tvState.setBackground(getResources().getDrawable(R.drawable.ic_bg_orange_corner));	
				} else {
					viewholder.tvState.setText("接受请求");
					viewholder.tvState.setTextColor(Color.GRAY);
					viewholder.tvState.setBackground(getResources().getDrawable(R.drawable.ic_bg_orange_corner));	
				}
				
				break;
			case 1:
				viewholder.tvState.setText(mContext.getResources().getString(R.string.status_passed));
				viewholder.tvState.setTextColor(Color.GRAY);
				viewholder.tvState.setBackground(null);
//				viewholder.tvState.setBackground(null);
				break;
			case 2:
				viewholder.tvState.setText(mContext.getResources().getString(R.string.status_refused));
				viewholder.tvState.setTextColor(Color.GRAY);
				viewholder.tvState.setBackground(null);
//				viewholder.tvState.setBackground(null);
				break;
			}
			
			return convertView;

		}



		void setLevelColor(int level,TextView tv, TextView tvLevel) {
			try {

				if(level == 0) {
					tvLevel.setTextColor(getResources().getColor(R.color.textview_title_listview));
					tv.setTextColor(getResources().getColor(R.color.textview_title_listview));	
				} else if(level == 1) {
					tvLevel.setTextColor(getResources().getColor(R.color.level_or));
					tv.setTextColor(getResources().getColor(R.color.level_or));
				} else {
					tvLevel.setTextColor(getResources().getColor(R.color.level_red));
					tv.setTextColor(getResources().getColor(R.color.level_red));
				}
			} catch(NullPointerException e) {

			}
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int[] mLocation = new int[2];
			v.getLocationInWindow(mLocation);
			NewSuperFriendsBean mDownloadListInfo = (NewSuperFriendsBean) v.getTag();
			if(mOnItemListViewEventListener != null) { // if we don't define this , there'll be the null pointer exception when we click an item if not setOnXXListener not work
				if(v.getId() == R.id.iv) {
					mOnItemListViewEventListener.onItemAvatarLisener(mDownloadListInfo, mLocation[0], mLocation[1]);
				} else if(v.getId() == R.id.linearlayout) {
					mOnItemListViewEventListener.onItemClickListener(mDownloadListInfo, mLocation[0], mLocation[1]);
				} else if(v.getId() == R.id.textview_state) {
					mOnItemListViewEventListener.onItemStateListener(mDownloadListInfo, mLocation[0], mLocation[1]);
				}
			}

		}
	}



	static class ViewHolder {
		ImageView ivAvatar;
		ImageView ivSex;
		TextView tvUserName;
		TextView tvSubTitle;
		TextView tvState;
	}
}